from Tkinter import *
from nipponsteel.gui.manager import ManagerProxy
from nipponsteel.gui.utils import Tooltip


def make_button(*args, **kwargs):
    """Makes a button with an associated tooltip."""
    tooltip = None
    if "tooltip" in kwargs:
        tooltip = kwargs.pop("tooltip")
    button = Button(*args, **kwargs)
    if tooltip is not None:
        Tooltip(button, message=tooltip)
    return button
    
    
class Controls(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        manager = self.manager = ManagerProxy(None)
        # Playback buttons ----------------------
        playback         = LabelFrame(self, text="Playback")
        self.rewind      = make_button(playback, width=3, text="|<<", 
                                       command=manager.rewind, tooltip="Rewind to beginning")
        self.play_back   = make_button(playback, width=3, text="<",
                                       command=manager.play_back, tooltip="Play backward")
        self.step_back   = make_button(playback, width=3, text="|<",
                                       command=manager.step_back, tooltip="Step backward")
        self.pause       = make_button(playback, width=3, text="||",
                                       command=manager.pause, tooltip="Pause")
        self.step        = make_button(playback, width=3, text=">|",
                                       command=manager.step, tooltip="Step forward")
        self.play        = make_button(playback, width=3, text=">",
                                       command=manager.play, tooltip="Play forward")
        self.forward     = make_button(playback, width=3, text=">>|", 
                                       command=manager.forward, tooltip="Forward to end")
        # Speed buttons -------------------------
        speed            = LabelFrame(self, text="Speed")
        self.speed_up    = make_button(speed, width=3, text="+",  
                                       command=manager.speed_up, tooltip="Speed up")
        self.speed_reset = make_button(speed, width=3, text="0",  
                                       command=manager.speed_reset, tooltip="Reset speed")
        self.speed_down  = make_button(speed, width=3, text="-", 
                                       command=manager.speed_down, tooltip="Speed down")
        # Zoom buttons --------------------------
        zoom             = LabelFrame(self, text="Zoom")
        self.zoom_in     = make_button(zoom, width=3, text="+",  
                                       command=manager.zoom_in, tooltip="Zoom in")
        self.zoom_reset  = make_button(zoom, width=3, text="0", 
                                       command=manager.zoom_reset, tooltip="Reset zoom")
        self.zoom_out    = make_button(zoom, width=3, text="-", 
                                       command=manager.zoom_out, tooltip="Zoom out")
        # Stack size buttons --------------------
        stack_size            = LabelFrame(self, text="Stack size")
        self.stack_size_up    = make_button(stack_size, width=3, text="+",  
                                            command=manager.stack_size_up, 
                                            tooltip="Increase stack size")
        self.stack_size_reset = make_button(stack_size, width=3, text="0",  
                                            command=manager.stack_size_reset, 
                                            tooltip="Reset stack size")
        self.stack_size_down  = make_button(stack_size, width=3, text="-", 
                                            command=manager.stack_size_down, 
                                            tooltip="Decrease stack size")
        # Button layout -------------------------
        self.columnconfigure(0, weight=1)
        self.columnconfigure(3, weight=1)
        
        playback.grid(row=0, column=0, sticky=N+S+E)
        speed.grid(row=0, column=1, sticky=N+S)
        zoom.grid(row=0, column=2, sticky=N+S)
        stack_size.grid(row=0, column=3, sticky=N+S+W)
        for button_list in [["rewind", "play_back", "step_back", 
                             "pause", "step", "play", "forward"],
                            ["speed_up", "speed_reset", "speed_down"],
                            ["zoom_in", "zoom_reset", "zoom_out"],
                            ["stack_size_up", "stack_size_reset", "stack_size_down"]]:
            for column, button in enumerate(button_list):
                getattr(self, button).grid(row=0, column=column)
                
